<?php
/**
 * This file is part of the Zhan Framework (http://zhan.com)
 * Copyright © 2015 Zhan, Inc. (http://zhan.com)
 */

namespace Zhan\Routing;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Zhan\Container\ServiceProvider;

class RouteServiceProvider extends ServiceProvider {

    /**
     * 注册服务至Application
     *
     * @return mixed
     */
    public function register()
    {
        $this->app['routes'] = function($app) {
            $loader = new YamlFileLoader(new FileLocator($app['path.config']));
            return $loader->load('routes.yml');
        };
        $this->app['requestContext'] = function() {
            return new RequestContext();
        };
        $this->app['matcher'] = function($app) {
            return new UrlMatcher($app['routes'], $app['requestContext']);
        };
        $this->app['resolver'] = function() {
            return new ControllerResolver();
        };
        $this->app['http_kernel'] = function($app) {
            return new HttpKernel($app['events'], $app['resolver']);
        };
    }
}